home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / amiga / gasldsrc.lha / gas-1.38 / write.c < prev    next >
C/C++ Source or Header  |  1992-04-17  |  39KB  |  1,269 lines

  1. /* write.c - emit .o file - Copyright(C)1986 Free Software Foundation, Inc.
  2.    Copyright (C) 1986,1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* 
  21.  
  22.    Umm, with real good luck, this thing should be set up to do byteordering
  23.    correctly, but I may have managed to miss a place or two.  Treat a.out
  24.    very carefully until you're SURE that it works. . .
  25.  
  26.    In order to cross-assemble the target machine must have an a.out header
  27.    similar to the one in a.out.h on THIS machine.  Byteorder doesn't matter;
  28.    we take special care of it, but the numbers must be the same SIZE (# of
  29.    bytes) and in the same PLACE.  If this is not true, you will have some
  30.    trouble.
  31.  */
  32.  
  33. #include "as.h"
  34. #include "md.h"
  35. #include "subsegs.h"
  36. #include "obstack.h"
  37. #include "struc-symbol.h"
  38. #include "write.h"
  39. #include "symbols.h"
  40.  
  41. #ifdef SPARC
  42. #include "sparc.h"
  43. #endif
  44. #ifdef I860
  45. #include "i860.h"
  46. #endif
  47.  
  48. void    append();
  49.  
  50. #ifdef hpux
  51. #define EXEC_MACHINE_TYPE HP9000S200_ID
  52. #endif
  53.  
  54. #ifdef DOT_LABEL_PREFIX
  55. #define LOCAL_LABEL(name) (name[0] =='.' \
  56.                          && ( name [1] == 'L' || name [1] == '.' ))
  57. #else  /* not defined DOT_LABEL_PREFIX */
  58. #define LOCAL_LABEL(name) (name [0] == 'L' )
  59. #endif /* not defined DOT_LABEL_PREFIX */
  60.  
  61. /*
  62.  * In: length of relocation (or of address) in chars: 1, 2 or 4.
  63.  * Out: GNU LD relocation length code: 0, 1, or 2.
  64.  */
  65.  
  66. static unsigned char
  67.  
  68. nbytes_r_length [] = {
  69.   42, 0, 1, 42, 2
  70.   };
  71.  
  72.  
  73. static struct frag *    text_frag_root;
  74. static struct frag *    data_frag_root;
  75.  
  76. static struct frag *    text_last_frag;    /* Last frag in segment. */
  77. static struct frag *    data_last_frag;    /* Last frag in segment. */
  78.  
  79. static struct exec    the_exec;
  80.  
  81. static long int string_byte_count;
  82.  
  83. static char *        the_object_file;
  84.  
  85. #if !defined(SPARC) && !defined(I860)
  86. static
  87. #endif
  88. char *        next_object_file_charP;    /* Tracks object file bytes. */
  89.  
  90. static long int        size_of_the_object_file; /* # bytes in object file. */
  91.  
  92. /* static long int        length; JF unused */    /* String length, including trailing '\0'. */
  93.  
  94. static void    relax_segment();
  95. void        emit_segment();
  96. static relax_addressT    relax_align();
  97. static long int    fixup_segment();
  98. #if !defined(SPARC) && !defined(I860)
  99. static void        emit_relocations();
  100. #endif
  101. /*
  102.  *            fix_new()
  103.  *
  104.  * Create a fixS in obstack 'notes'.
  105.  */
  106. void
  107. #if defined(SPARC) || defined(I860)
  108. fix_new (frag, where, size, add_symbol, sub_symbol, offset, pcrel, r_type)
  109. #else
  110. fix_new (frag, where, size, add_symbol, sub_symbol, offset, pcrel, baserel)
  111. #endif
  112.      fragS *    frag;        /* Which frag? */
  113.      int    where;        /* Where in that frag? */
  114.      short int    size;        /* 1, 2  or 4 usually. */
  115.      symbolS *    add_symbol;    /* X_add_symbol. */
  116.      symbolS *    sub_symbol;    /* X_subtract_symbol. */
  117.      long int    offset;        /* X_add_number. */
  118.      int    pcrel;        /* TRUE if PC-relative relocation. */
  119.      int    baserel;        /* true if base-relative data */
  120. #if defined(SPARC) || defined(I860)
  121.     int        r_type;
  122. #endif
  123. {
  124.   register fixS *    fixP;
  125.  
  126.   fixP = (fixS *)obstack_alloc(¬es,sizeof(fixS));
  127.  
  128.   fixP -> fx_frag    = frag;
  129.   fixP -> fx_where    = where;
  130.   fixP -> fx_size    = size;
  131.   fixP -> fx_addsy    = add_symbol;
  132.   fixP -> fx_subsy    = sub_symbol;
  133.   fixP -> fx_offset    = offset;
  134.   fixP -> fx_pcrel    = pcrel;
  135.   fixP -> fx_next    = * seg_fix_rootP;
  136.  
  137.   /* JF these 'cuz of the NS32K stuff */
  138.   fixP -> fx_im_disp    = 0;
  139.   fixP -> fx_pcrel_adjust = 0;
  140.   fixP -> fx_bsr    = baserel;
  141.   fixP ->fx_bit_fixP    = 0;
  142.  
  143. #if defined(SPARC) || defined(I860)
  144.   fixP->fx_r_type = r_type;
  145. #endif
  146.  
  147.   * seg_fix_rootP = fixP;
  148. }
  149.  
  150. void
  151. write_object_file()
  152. {
  153.   register struct frchain *    frchainP; /* Track along all frchains. */
  154.   register fragS *        fragP;    /* Track along all frags. */
  155.   register struct frchain *    next_frchainP;
  156.   register fragS * *        prev_fragPP;
  157.   register char *        name;
  158.   register symbolS *        symbolP;
  159.   register symbolS **        symbolPP;
  160.   /* register fixS *        fixP; JF unused */
  161.   unsigned
  162.       text_siz,
  163.     data_siz,
  164.     syms_siz,
  165.     tr_siz,
  166.     dr_siz;
  167.   void output_file_create();
  168.   void output_file_append();
  169.   void output_file_close();
  170. #ifdef DONTDEF
  171.   void gdb_emit();
  172.   void gdb_end();
  173. #endif
  174.   extern short omagic;        /* JF magic # to write out.  Is different for
  175.                    Suns and Vaxen and other boxes */
  176.   extern short mid;
  177.  
  178. #ifdef    VMS
  179.   /*
  180.    *    Under VMS we try to be compatible with VAX-11 "C".  Thus, we
  181.    *    call a routine to check for the definition of the procedure
  182.    *    "_main", and if so -- fix it up so that it can be program
  183.    *    entry point.
  184.    */
  185.   VMS_Check_For_Main();
  186. #endif /* VMS */
  187.   /*
  188.    * After every sub-segment, we fake an ".align ...". This conforms to BSD4.2
  189.    * brane-damage. We then fake ".fill 0" because that is the kind of frag
  190.    * that requires least thought. ".align" frags like to have a following
  191.    * frag since that makes calculating their intended length trivial.
  192.    */
  193. #define SUB_SEGMENT_ALIGN (2)
  194.   for ( frchainP=frchain_root; frchainP; frchainP=frchainP->frch_next )
  195.     {
  196. #ifdef    VMS
  197.       /*
  198.        *    Under VAX/VMS, the linker (and PSECT specifications)
  199.        *    take care of correctly aligning the segments.
  200.        *    Doing the alignment here (on initialized data) can
  201.        *    mess up the calculation of global data PSECT sizes.
  202.        */
  203. #undef    SUB_SEGMENT_ALIGN
  204. #define    SUB_SEGMENT_ALIGN ((frchainP->frch_seg != SEG_DATA) ? 2 : 0)
  205. #endif    /* VMS */
  206.       subseg_new (frchainP -> frch_seg, frchainP -> frch_subseg);
  207.       frag_align (SUB_SEGMENT_ALIGN, 0);
  208.                 /* frag_align will have left a new frag. */
  209.                 /* Use this last frag for an empty ".fill". */
  210.       /*
  211.        * For this segment ...
  212.        * Create a last frag. Do not leave a "being filled in frag".
  213.        */
  214.       frag_wane (frag_now);
  215.       frag_now -> fr_fix    = 0;
  216.       know( frag_now -> fr_next == NULL );
  217.       /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
  218.       /* Above shows we haven't left a half-completed object on obstack. */
  219.     }
  220.  
  221.   /*
  222.    * From now on, we don't care about sub-segments.
  223.    * Build one frag chain for each segment. Linked thru fr_next.
  224.    * We know that there is at least 1 text frchain & at least 1 data frchain.
  225.    */
  226.   prev_fragPP = &text_frag_root;
  227.   for ( frchainP=frchain_root; frchainP; frchainP=next_frchainP )
  228.     {
  229.       know( frchainP -> frch_root );
  230.       * prev_fragPP = frchainP -> frch_root;
  231.       prev_fragPP = & frchainP -> frch_last -> fr_next;
  232.       if (   ((next_frchainP = frchainP->frch_next) == NULL)
  233.       || next_frchainP == data0_frchainP)
  234.     {
  235.       prev_fragPP = & data_frag_root;
  236.       if ( next_frchainP )
  237.         {
  238.           text_last_frag = frchainP -> frch_last;
  239.         }
  240.       else
  241.         {
  242.           data_last_frag = frchainP -> frch_last;
  243.         }
  244.     }
  245.     }                /* for(each struct frchain) */
  246.  
  247.   /*
  248.    * We have two segments. If user gave -R flag, then we must put the
  249.    * data frags into the text segment. Do this before relaxing so
  250.    * we know to take advantage of -R and make shorter addresses.
  251.    */
  252.   if ( flagseen [ 'R' ] )
  253.     {
  254.       fixS *tmp;
  255.  
  256.       text_last_frag -> fr_next = data_frag_root;
  257.       text_last_frag = data_last_frag;
  258.       data_last_frag = NULL;
  259.       data_frag_root = NULL;
  260.       if(text_fix_root) {
  261.     for(tmp=text_fix_root;tmp->fx_next;tmp=tmp->fx_next)
  262.       ;
  263.     tmp->fx_next=data_fix_root;
  264.       } else
  265.         text_fix_root=data_fix_root;
  266.       data_fix_root=NULL;
  267.     }
  268.  
  269.   relax_segment (text_frag_root, SEG_TEXT);
  270.   relax_segment (data_frag_root, SEG_DATA);
  271.   /*
  272.    * Now the addresses of frags are correct within the segment.
  273.    */
  274.  
  275.   know(   text_last_frag -> fr_type   == rs_fill && text_last_frag -> fr_offset == 0 );
  276.   text_siz=text_last_frag->fr_address;
  277. #ifdef SPARC
  278.   text_siz= (text_siz+7)&(~7);
  279.   text_last_frag->fr_address=text_siz;
  280. #endif
  281.   md_number_to_chars((char *)&the_exec.a_text,text_siz, sizeof(the_exec.a_text));
  282.   /* the_exec . a_text = text_last_frag -> fr_address; */
  283.  
  284.   /*
  285.    * Join the 2 segments into 1 huge segment.
  286.    * To do this, re-compute every rn_address in the SEG_DATA frags.
  287.    * Then join the data frags after the text frags.
  288.    *
  289.    * Determine a_data [length of data segment].
  290.    */
  291.   if (data_frag_root)
  292.     {
  293.       register relax_addressT    slide;
  294.  
  295.       know(   text_last_frag -> fr_type   == rs_fill && text_last_frag -> fr_offset == 0 );
  296.       data_siz=data_last_frag->fr_address;
  297. #ifdef SPARC
  298.       data_siz += (8 - (data_siz % 8)) % 8;
  299.       data_last_frag->fr_address = data_siz;
  300. #endif
  301.       md_number_to_chars((char *)&the_exec.a_data,data_siz,sizeof(the_exec.a_data));
  302.       /* the_exec . a_data = data_last_frag -> fr_address; */
  303.       slide = text_siz; /* Address in file of the data segment. */
  304.       for (fragP = data_frag_root;
  305.        fragP;
  306.        fragP = fragP -> fr_next)
  307.     {
  308.       fragP -> fr_address += slide;
  309.     }
  310.       know( text_last_frag );
  311.       text_last_frag -> fr_next = data_frag_root;
  312.     }
  313.   else {
  314.       md_number_to_chars((char *)&the_exec.a_data,0,sizeof(the_exec.a_data));
  315.       data_siz = 0;
  316.   }
  317.  
  318.   bss_address_frag . fr_address = text_siz + data_siz;
  319. #ifdef SPARC
  320.   local_bss_counter=(local_bss_counter+7)&(~7);
  321. #endif
  322.   md_number_to_chars((char *)&the_exec.a_bss,local_bss_counter,sizeof(the_exec.a_bss));
  323.  
  324.           
  325.   /*
  326.    *
  327.    * Crawl the symbol chain.
  328.    *
  329.    * For each symbol whose value depends on a frag, take the address of
  330.    * that frag and subsume it into the value of the symbol.
  331.    * After this, there is just one way to lookup a symbol value.
  332.    * Values are left in their final state for object file emission.
  333.    * We adjust the values of 'L' local symbols, even if we do
  334.    * not intend to emit them to the object file, because their values
  335.    * are needed for fix-ups.
  336.    *
  337.    * Unless we saw a -L flag, remove all symbols that begin with 'L'
  338.    * from the symbol chain.
  339.    *
  340.    * Count the (length of the nlists of the) (remaining) symbols.
  341.    * Assign a symbol number to each symbol.
  342.    * Count the number of string-table chars we will emit.
  343.    *
  344.    */
  345.   know( zero_address_frag . fr_address == 0 );
  346.   string_byte_count = sizeof( string_byte_count );
  347.  
  348.   /* JF deal with forward references first. . . */
  349.   for(symbolP=symbol_rootP;symbolP;symbolP=symbolP->sy_next) {
  350.       if(symbolP->sy_forward) {
  351.         symbolP->sy_value+=symbolP->sy_forward->sy_value+symbolP->sy_forward->sy_frag->fr_address;
  352.         symbolP->sy_forward=0;
  353.     }
  354.   }
  355.   symbolPP = & symbol_rootP;    /* -> last symbol chain link. */
  356.   {
  357.     register long int        symbol_number;
  358.  
  359.     symbol_number = 0;
  360.     while (symbolP  = * symbolPP)
  361.       {
  362.     name = symbolP -> sy_name;
  363.     if(flagseen['R'] && (symbolP->sy_nlist.n_type&N_DATA)) {
  364.       symbolP->sy_nlist.n_type&= ~N_DATA;
  365.       symbolP->sy_nlist.n_type|= N_TEXT;
  366.     }
  367.     /* if(symbolP->sy_forward) {
  368.       symbolP->sy_value += symbolP->sy_forward->sy_value + symbolP->sy_forward->sy_frag->fr_address;
  369.     } */
  370.     
  371.     symbolP -> sy_value += symbolP -> sy_frag -> fr_address;
  372.         /* JF the 128 bit is a hack so stabs like
  373.            "LET_STMT:23. . ."  don't go away */
  374.     /* CPH: 128 bit hack is moby loser.  N_SO for file "Lower.c"
  375.        fell through the cracks.  I think that N_STAB should be
  376.        used instead of 128. */
  377.         /* JF the \001 bit is to make sure that local labels
  378.            ( 1: - 9: don't make it into the symtable either */
  379. #ifndef    VMS    /* Under VMS we need to keep local symbols */
  380.     if ( !name || (symbolP->sy_nlist.n_type&N_STAB)
  381.         || (name[2]!='\001' && (flagseen ['L'] || ! LOCAL_LABEL(name) )))
  382. #endif    /* not VMS */
  383.       {
  384.         symbolP -> sy_number = symbol_number ++;
  385. #ifndef    VMS
  386.         if (name)
  387.           {            /* Ordinary case. */
  388.         symbolP -> sy_name_offset = string_byte_count;
  389.         string_byte_count += strlen (symbolP  -> sy_name) + 1;
  390.           }
  391.         else            /* .Stabd case. */
  392. #endif    /* not VMS */
  393.         symbolP -> sy_name_offset = 0;
  394.         symbolPP = & (symbolP -> sy_next);
  395.       }
  396. #ifndef    VMS
  397.     else
  398.         * symbolPP = symbolP -> sy_next;
  399. #endif    /* not VMS */
  400.       }                /* for each symbol */
  401.  
  402.     syms_siz = sizeof( struct nlist) * symbol_number;
  403.     md_number_to_chars((char *)&the_exec.a_syms,syms_siz,sizeof(the_exec.a_syms));
  404.     /* the_exec . a_syms = sizeof( struct nlist) * symbol_number; */
  405.   }
  406.  
  407.   /*
  408.    * Addresses of frags now reflect addresses we use in the object file.
  409.    * Symbol values are correct.
  410.    * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
  411.    * Also converting any machine-dependent frags using md_convert_frag();
  412.    */
  413.   subseg_change( SEG_TEXT, 0);
  414.  
  415.   for (fragP = text_frag_root;  fragP;  fragP = fragP -> fr_next)
  416.     {
  417.       switch (fragP -> fr_type)
  418.     {
  419.     case rs_align:
  420.     case rs_org:
  421.       fragP -> fr_type = rs_fill;
  422.       know( fragP -> fr_var == 1 );
  423.       know( fragP -> fr_next );
  424.       fragP -> fr_offset
  425.         =     fragP -> fr_next -> fr_address
  426.           -   fragP -> fr_address
  427.         - fragP -> fr_fix;
  428.       break;
  429.  
  430.     case rs_fill:
  431.       break;
  432.  
  433.     case rs_machine_dependent:
  434.       md_convert_frag (fragP);
  435.       /*
  436.        * After md_convert_frag, we make the frag into a ".space 0".
  437.        * Md_convert_frag() should set up any fixSs and constants
  438.        * required.
  439.        */
  440.       frag_wane (fragP);
  441.       break;
  442.  
  443. #ifndef WORKING_DOT_WORD
  444.     case rs_broken_word:
  445.       {
  446.         struct broken_word *lie;
  447.         extern md_short_jump_size;
  448.         extern md_long_jump_size;
  449.  
  450.         if(fragP->fr_subtype) {
  451.           fragP->fr_fix+=md_short_jump_size;
  452.           for(lie=(struct broken_word *)(fragP->fr_symbol);lie && lie->dispfrag==fragP;lie=lie->next_broken_word)
  453.         if(lie->added==1)
  454.           fragP->fr_fix+=md_long_jump_size;
  455.         }
  456.         frag_wane(fragP);
  457.       }
  458.       break;
  459. #endif
  460.  
  461.     default:
  462.       BAD_CASE( fragP -> fr_type );
  463.       break;
  464.     }            /* switch (fr_type) */
  465.     }                /* for each frag. */
  466.  
  467. #ifndef WORKING_DOT_WORD
  468.     {
  469.       struct broken_word *lie;
  470.       struct broken_word **prevP;
  471.  
  472.       prevP= &broken_words;
  473.       for(lie=broken_words; lie; lie=lie->next_broken_word)
  474.     if(!lie->added) {
  475. #if defined(SPARC) || defined(I860)
  476.       fix_new(    lie->frag,  lie->word_goes_here - lie->frag->fr_literal,
  477.               2,  lie->add,
  478.             lie->sub,  lie->addnum,
  479.             0,  NO_RELOC);
  480. #endif
  481. #ifdef NS32K
  482.       fix_new_ns32k(lie->frag,
  483.               lie->word_goes_here - lie->frag->fr_literal,
  484.             2,
  485.             lie->add,
  486.             lie->sub,
  487.             lie->addnum,
  488.             0, 0, 2, 0, 0);
  489. #endif
  490. #if !defined(SPARC) && !defined(NS32K) && !defined(I860)
  491.       fix_new(    lie->frag,  lie->word_goes_here - lie->frag->fr_literal,
  492.               2,  lie->add,
  493.             lie->sub,  lie->addnum,
  494.             0, 0);
  495. #endif
  496.       /* md_number_to_chars(lie->word_goes_here,
  497.                    lie->add->sy_value
  498.                    + lie->addnum
  499.                    - (lie->sub->sy_value),
  500.                  2); */
  501.       *prevP=lie->next_broken_word;
  502.     } else
  503.       prevP= &(lie->next_broken_word);
  504.  
  505.       for(lie=broken_words;lie;) {
  506.     struct broken_word *untruth;
  507.     char    *table_ptr;
  508.     long    table_addr;
  509.     long    from_addr,
  510.         to_addr;
  511.     int    n,
  512.         m;
  513.  
  514.     extern    md_short_jump_size;
  515.     extern    md_long_jump_size;
  516.     void    md_create_short_jump();
  517.     void    md_create_long_jump();
  518.  
  519.  
  520.  
  521.     fragP=lie->dispfrag;
  522.  
  523.     /* Find out how many broken_words go here */
  524.     n=0;
  525.     for(untruth=lie;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word)
  526.       if(untruth->added==1)
  527.         n++;
  528.  
  529.     table_ptr=lie->dispfrag->fr_opcode;
  530.     table_addr=lie->dispfrag->fr_address+(table_ptr - lie->dispfrag->fr_literal);
  531.     /* Create the jump around the long jumps */
  532.     /* This is a short jump from table_ptr+0 to table_ptr+n*long_jump_size */
  533.     from_addr=table_addr;
  534.     to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
  535.     md_create_short_jump(table_ptr,from_addr,to_addr,lie->dispfrag,lie->add);
  536.     table_ptr+=md_short_jump_size;
  537.     table_addr+=md_short_jump_size;
  538.  
  539.     for(m=0;lie && lie->dispfrag==fragP;m++,lie=lie->next_broken_word) {
  540.       if(lie->added==2)
  541.         continue;
  542.       /* Patch the jump table */
  543.       /* This is the offset from ??? to table_ptr+0 */
  544.       to_addr =   table_addr
  545.                 - (lie->sub->sy_value);
  546.       md_number_to_chars(lie->word_goes_here,to_addr,2);
  547.       for(untruth=lie->next_broken_word;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word) {
  548.         if(untruth->use_jump==lie)
  549.           md_number_to_chars(untruth->word_goes_here,to_addr,2);
  550.       }
  551.  
  552.       /* Install the long jump */
  553.       /* this is a long jump from table_ptr+0 to the final target */
  554.       from_addr=table_addr;
  555.       to_addr=lie->add->sy_value+lie->addnum;
  556.       md_create_long_jump(table_ptr,from_addr,to_addr,lie->dispfrag,lie->add);
  557.       table_ptr+=md_long_jump_size;
  558.       table_addr+=md_long_jump_size;
  559.     }
  560.       }
  561.     }
  562. #endif
  563. #ifndef    VMS
  564.   /*
  565.    * Scan every FixS performing fixups. We had to wait until now to do
  566.    * this because md_convert_frag() may have made some fixSs.
  567.    */
  568.   /* the_exec . a_trsize
  569.     = sizeof(struct relocation_info) * fixup_segment (text_fix_root, N_TEXT);
  570.   the_exec . a_drsize
  571.     = sizeof(struct relocation_info) * fixup_segment (data_fix_root, N_DATA); */
  572.  
  573.   tr_siz=sizeof(struct relocation_info) * fixup_segment (text_fix_root, N_TEXT);
  574.   md_number_to_chars((char *)&the_exec.a_trsize, tr_siz ,sizeof(the_exec.a_trsize));
  575.   dr_siz=sizeof(struct relocation_info) * fixup_segment (data_fix_root, N_DATA);
  576.   md_number_to_chars((char *)&the_exec.a_drsize, dr_siz, sizeof(the_exec.a_drsize));
  577.   md_number_to_chars((char *)&the_exec.a_mid,mid,sizeof(the_exec.a_mid));
  578.   md_number_to_chars((char *)&the_exec.a_magic,omagic,sizeof(the_exec.a_magic));
  579.   md_number_to_chars((char *)&the_exec.a_entry,0,sizeof(the_exec.a_entry));
  580.  
  581. #ifdef EXEC_MACHINE_TYPE
  582.   md_number_to_chars((char *)&the_exec.a_machtype, EXEC_MACHINE_TYPE, sizeof(the_exec.a_machtype));
  583. #endif
  584. #ifdef EXEC_VERSION
  585.   md_number_to_chars((char *)&the_exec.a_version,EXEC_VERSION,sizeof(the_exec.a_version));
  586. #endif
  587.   
  588.   /* the_exec . a_entry = 0; */
  589.  
  590.   size_of_the_object_file =
  591.     sizeof( the_exec ) +
  592.       text_siz +
  593.         data_siz +
  594.       syms_siz +
  595.         tr_siz +
  596.           dr_siz +
  597.         string_byte_count;
  598.     
  599.   next_object_file_charP
  600.     = the_object_file
  601.       = xmalloc ( size_of_the_object_file );
  602.  
  603.   output_file_create (out_file_name);
  604.  
  605.   append (& next_object_file_charP, (char *)(&the_exec), (unsigned long)sizeof(the_exec));
  606.  
  607.   /*
  608.    * Emit code.
  609.    */
  610.   for (fragP = text_frag_root;  fragP;  fragP = fragP -> fr_next)
  611.     {
  612.       register long int        count;
  613.       register char *        fill_literal;
  614.       register long int        fill_size;
  615.  
  616.       know( fragP -> fr_type == rs_fill );
  617.       append (& next_object_file_charP, fragP -> fr_literal, (unsigned long)fragP -> fr_fix);
  618.       fill_literal= fragP -> fr_literal + fragP -> fr_fix;
  619.       fill_size   = fragP -> fr_var;
  620.       know( fragP -> fr_offset >= 0 );
  621.       for (count = fragP -> fr_offset;  count;  count --)
  622.       append (& next_object_file_charP, fill_literal, (unsigned long)fill_size);
  623.     }                /* for each code frag. */
  624.  
  625.   /*
  626.    * Emit relocations.
  627.    */
  628.   emit_relocations (text_fix_root, (relax_addressT)0);
  629.   emit_relocations (data_fix_root, text_last_frag -> fr_address);
  630.   /*
  631.    * Emit all symbols left in the symbol chain.
  632.    * Any symbol still undefined is made N_EXT.
  633.    */
  634.   for (   symbolP = symbol_rootP;   symbolP;   symbolP = symbolP -> sy_next   )
  635.     {
  636.       register char *    temp;
  637.  
  638.       temp = symbolP -> sy_nlist . n_un . n_name;
  639.       /* JF fix the numbers up. Call by value RULES! */
  640.       md_number_to_chars((char *)&(symbolP -> sy_nlist  . n_un . n_strx ),symbolP -> sy_name_offset,sizeof(symbolP -> sy_nlist  . n_un . n_strx ));
  641.       md_number_to_chars((char *)&(symbolP->sy_nlist.n_desc),symbolP->sy_nlist.n_desc,sizeof(symbolP -> sy_nlist  . n_desc));
  642.       md_number_to_chars((char *)&(symbolP->sy_nlist.n_value),symbolP->sy_nlist.n_value,sizeof(symbolP->sy_nlist.n_value));
  643.       /* symbolP -> sy_nlist  . n_un . n_strx = symbolP -> sy_name_offset; JF replaced by md above */
  644.       if (symbolP -> sy_type == N_UNDF)
  645.       symbolP -> sy_type |= N_EXT; /* Any undefined symbols become N_EXT. */
  646.       append (& next_object_file_charP, (char *)(& symbolP -> sy_nlist),
  647.           (unsigned long)sizeof(struct nlist));
  648.       symbolP -> sy_nlist . n_un . n_name = temp;
  649.     }                /* for each symbol */
  650.  
  651.   /*
  652.    * next_object_file_charP -> slot for next object byte.
  653.    * Emit strings.
  654.    * Find strings by crawling along symbol table chain.
  655.    */
  656. /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
  657.   md_number_to_chars((char *)&string_byte_count, string_byte_count, sizeof(string_byte_count));
  658.  
  659.   append (& next_object_file_charP, (char *)&string_byte_count, (unsigned long)sizeof(string_byte_count));
  660.   for (   symbolP = symbol_rootP;   symbolP;   symbolP = symbolP -> sy_next   )
  661.     {
  662.       if (symbolP -> sy_name)
  663.     {            /* Ordinary case: not .stabd. */
  664.       append (& next_object_file_charP, symbolP -> sy_name,
  665.           (unsigned long)(strlen (symbolP -> sy_name) + 1));
  666.     }
  667.     }                /* for each symbol */
  668.  
  669.   know( next_object_file_charP == the_object_file + size_of_the_object_file );
  670.  
  671.   output_file_append (the_object_file, size_of_the_object_file, out_file_name);
  672.  
  673. #ifdef DONTDEF
  674.   if (flagseen['G'])        /* GDB symbol file to be appended? */
  675.     {
  676.       gdb_emit (out_file_name);
  677.       gdb_end ();
  678.     }
  679. #endif
  680.  
  681.   output_file_close (out_file_name);
  682. #else    /* VMS */
  683.   /*
  684.    *    Now do the VMS-dependent part of writing the object file
  685.    */
  686.   VMS_write_object_file(text_siz, data_siz, text_frag_root, data_frag_root);
  687. #endif    /* VMS */
  688. }                /* write_object_file() */
  689.  
  690. /*
  691.  *            relax_segment()
  692.  *
  693.  * Now we have a segment, not a crowd of sub-segments, we can make fr_address
  694.  * values.
  695.  *
  696.  * Relax the frags.
  697.  *
  698.  * After this, all frags in this segment have addresses that are correct
  699.  * within the segment. Since segments live in different file addresses,
  700.  * these frag addresses may not be the same as final object-file addresses.
  701.  */
  702. #ifndef    VMS
  703. static
  704. #endif    /* not VMS */
  705. void
  706. relax_segment (segment_frag_root, segment_type)
  707.      struct frag *    segment_frag_root;
  708.      segT        segment_type; /* N_DATA or N_TEXT */
  709. {
  710.   register struct frag *    fragP;
  711.   register relax_addressT    address;
  712.   /* register relax_addressT    old_address; JF unused */
  713.   /* register relax_addressT    new_address; JF unused */
  714.  
  715.   know( segment_type == SEG_DATA || segment_type == SEG_TEXT );
  716.  
  717.   /* In case md_estimate_size_before_relax() wants to make fixSs. */
  718.   subseg_change (segment_type, 0);
  719.  
  720.   /*
  721.    * For each frag in segment: count and store  (a 1st guess of) fr_address.
  722.    */
  723.   address = 0;
  724.   for ( fragP = segment_frag_root;   fragP;   fragP = fragP -> fr_next )
  725.     {
  726.       fragP -> fr_address = address;
  727.       address += fragP -> fr_fix;
  728.       switch (fragP -> fr_type)
  729.     {
  730.     case rs_fill:
  731.       address += fragP -> fr_offset * fragP -> fr_var;
  732.       break;
  733.  
  734.     case rs_align:
  735.       address += relax_align (address, fragP -> fr_offset);
  736.       break;
  737.  
  738.     case rs_org:
  739.       /*
  740.        * Assume .org is nugatory. It will grow with 1st relax.
  741.        */
  742.       break;
  743.  
  744.     case rs_machine_dependent:
  745.       address += md_estimate_size_before_relax
  746.         (fragP, seg_N_TYPE [(int) segment_type]);
  747.       break;
  748.  
  749. #ifndef WORKING_DOT_WORD
  750.         /* Broken words don't concern us yet */
  751.     case rs_broken_word:
  752.         break;
  753. #endif
  754.  
  755.     default:
  756.       BAD_CASE( fragP -> fr_type );
  757.       break;
  758.     }            /* switch(fr_type) */
  759.     }                /* for each frag in the segment */
  760.  
  761.   /*
  762.    * Do relax().
  763.    */
  764.   {
  765.     register long int    stretch; /* May be any size, 0 or negative. */
  766.                 /* Cumulative number of addresses we have */
  767.                 /* relaxed this pass. */
  768.                 /* We may have relaxed more than one address. */
  769.     register long int stretched;  /* Have we stretched on this pass? */
  770.                   /* This is 'cuz stretch may be zero, when,
  771.                      in fact some piece of code grew, and
  772.                      another shrank.  If a branch instruction
  773.                      doesn't fit anymore, we could be scrod */
  774.  
  775.     do
  776.       {
  777.     stretch = stretched = 0;
  778.     for (fragP = segment_frag_root;  fragP;  fragP = fragP -> fr_next)
  779.       {
  780.         register long int    growth;
  781.         register long int    was_address;
  782.         /* register long int    var; */
  783.         register long int    offset;
  784.         register symbolS *    symbolP;
  785.         register long int    target;
  786.         register long int    after;
  787.         register long int    aim;
  788.  
  789.         was_address = fragP -> fr_address;
  790.         address = fragP -> fr_address += stretch;
  791.         symbolP = fragP -> fr_symbol;
  792.         offset = fragP -> fr_offset;
  793.         /* var = fragP -> fr_var; */
  794.         switch (fragP -> fr_type)
  795.           {
  796.           case rs_fill:    /* .fill never relaxes. */
  797.         growth = 0;
  798.         break;
  799.  
  800. #ifndef WORKING_DOT_WORD
  801.         /* JF:  This is RMS's idea.  I do *NOT* want to be blamed
  802.            for it I do not want to write it.  I do not want to have
  803.            anything to do with it.  This is not the proper way to
  804.            implement this misfeature. */
  805.           case rs_broken_word:
  806.             {
  807.         struct broken_word *lie;
  808.         struct broken_word *untruth;
  809.         extern int md_short_jump_size;
  810.         extern int md_long_jump_size;
  811.  
  812.             /* Yes this is ugly (storing the broken_word pointer
  813.                in the symbol slot).  Still, this whole chunk of
  814.                code is ugly, and I don't feel like doing anything
  815.                about it.  Think of it as stubbornness in action */
  816.         growth=0;
  817.             for(lie=(struct broken_word *)(fragP->fr_symbol);
  818.             lie && lie->dispfrag==fragP;
  819.             lie=lie->next_broken_word) {
  820.  
  821.             if(lie->added)
  822.                 continue;
  823.             offset=  lie->add->sy_frag->fr_address+lie->add->sy_value + lie->addnum -
  824.                 (lie->sub->sy_frag->fr_address+lie->sub->sy_value);
  825.             if(offset<=-32768 || offset>=32767) {
  826.                 if(flagseen['k'])
  827.                     as_warn(".word %s-%s+%ld didn't fit",lie->add->sy_name,lie->sub->sy_name,lie->addnum);
  828.                 lie->added=1;
  829.                 if(fragP->fr_subtype==0) {
  830.                     fragP->fr_subtype++;
  831.                     growth+=md_short_jump_size;
  832.                 }
  833.                 for(untruth=lie->next_broken_word;untruth && untruth->dispfrag==lie->dispfrag;untruth=untruth->next_broken_word)
  834.                     if(untruth->add->sy_frag==lie->add->sy_frag && untruth->add->sy_value==lie->add->sy_value) {
  835.                         untruth->added=2;
  836.                         untruth->use_jump=lie;
  837.                     }
  838.                 growth+=md_long_jump_size;
  839.             }
  840.             }
  841.         }
  842.         break;
  843. #endif
  844.           case rs_align:
  845.         growth = relax_align ((relax_addressT)(address + fragP -> fr_fix), offset)
  846.           - relax_align ((relax_addressT)(was_address +  fragP -> fr_fix), offset);
  847.         break;
  848.  
  849.           case rs_org:
  850.         target = offset;
  851.         if (symbolP)
  852.           {
  853.             know(   ((symbolP -> sy_type & N_TYPE) == N_ABS) || ((symbolP -> sy_type & N_TYPE) == N_DATA) || ((symbolP -> sy_type & N_TYPE) == N_TEXT));
  854.             know( symbolP -> sy_frag );
  855.             know( (symbolP->sy_type&N_TYPE)!=N_ABS || symbolP->sy_frag==&zero_address_frag );
  856.             target +=
  857.               symbolP -> sy_value
  858.             + symbolP -> sy_frag -> fr_address;
  859.           }
  860.         know( fragP -> fr_next );
  861.         after = fragP -> fr_next -> fr_address;
  862.         growth = ((target - after ) > 0) ? (target - after) : 0;
  863.                 /* Growth may be -ve, but variable part */
  864.                 /* of frag cannot have < 0 chars. */
  865.                 /* That is, we can't .org backwards. */
  866.  
  867.         growth -= stretch;    /* This is an absolute growth factor */
  868.         break;
  869.  
  870.           case rs_machine_dependent:
  871.         {
  872.           register const relax_typeS *    this_type;
  873.           register const relax_typeS *    start_type;
  874.           register relax_substateT    next_state;
  875.           register relax_substateT    this_state;
  876.  
  877.           start_type = this_type
  878.             = md_relax_table + (this_state = fragP -> fr_subtype);
  879.         target = offset;
  880.         if (symbolP)
  881.           {
  882.  know(   ((symbolP -> sy_type & N_TYPE) == N_ABS) || ((symbolP -> sy_type &
  883.  N_TYPE) == N_DATA) || ((symbolP -> sy_type & N_TYPE) == N_TEXT));
  884.             know( symbolP -> sy_frag );
  885.             know( (symbolP->sy_type&N_TYPE)!=N_ABS || symbolP->sy_frag==&zero_address_frag );
  886.             target +=
  887.               symbolP -> sy_value
  888.             + symbolP -> sy_frag -> fr_address;
  889.  
  890.             /* If frag has yet to be reached on this pass,
  891.                assume it will move by STRETCH just as we did.
  892.                If this is not so, it will be because some frag
  893.                between grows, and that will force another pass.  */
  894.  
  895.                    /* JF was just address */
  896.                 /* JF also added is_dnrange hack */
  897.                 /* There's gotta be a better/faster/etc way
  898.                    to do this. . . */
  899.             /* gnu@cygnus.com:  I changed this from > to >=
  900.                because I ran into a zero-length frag (fr_fix=0)
  901.                which was created when the obstack needed a new
  902.                chunk JUST AFTER the opcode of a branch.  Since
  903.                fr_fix is zero, fr_address of this frag is the same
  904.                as fr_address of the next frag.  This
  905.                zero-length frag was variable and jumped to .+2
  906.                (in the next frag), but since the > comparison
  907.                below failed (the two were =, not >), "stretch"
  908.                was not added to the target.  Stretch was 178, so
  909.                the offset appeared to be .-176 instead, which did
  910.                not fit into a byte branch, so the assembler
  911.                relaxed the branch to a word.  This didn't compare
  912.                with what happened when the same source file was
  913.                assembled on other machines, which is how I found it.
  914.                You might want to think about what other places have
  915.                trouble with zero length frags... */
  916.  
  917.             if (symbolP->sy_frag->fr_address >= was_address && is_dnrange(fragP,symbolP->sy_frag))
  918.               target += stretch;
  919.  
  920.           }
  921.           aim = target - address - fragP -> fr_fix;
  922.           /* The displacement is affected by the instruction size
  923.            * for the 32k architecture. I think we ought to be able
  924.            * to add fragP->fr_pcrel_adjust in all cases (it should be
  925.            * zero if not used), but just in case it breaks something
  926.            * else we'll put this inside #ifdef NS32K ... #endif
  927.            */
  928. #ifdef NS32K
  929.           aim += fragP->fr_pcrel_adjust;
  930. #endif
  931.  
  932.           if (aim < 0)
  933.             {
  934.               /* Look backwards. */
  935.               for (next_state = this_type -> rlx_more;  next_state;  )
  936.             {
  937.               if (aim >= this_type -> rlx_backward)
  938.                   next_state = 0;
  939.               else
  940.                 {    /* Grow to next state. */
  941.                   this_type = md_relax_table + (this_state = next_state);
  942.                   next_state = this_type -> rlx_more;
  943.                 }
  944.             }
  945.             }
  946.           else
  947.             {
  948. #ifdef DONTDEF
  949. /* JF these next few lines of code are for the mc68020 which can't handle short
  950.    offsets of zero in branch instructions.  What a kludge! */
  951.  if(aim==0 && this_state==(1<<2+0)) {    /* FOO hard encoded from m.c */
  952.     aim=this_type->rlx_forward+1;    /* Force relaxation into word mode */
  953.  }
  954. #endif
  955. /* JF end of 68020 code */
  956.               /* Look forwards. */
  957.               for (next_state = this_type -> rlx_more;  next_state;  )
  958.             {
  959.               if (aim <= this_type -> rlx_forward)
  960.                   next_state = 0;
  961.               else
  962.                 {    /* Grow to next state. */
  963.                   this_type = md_relax_table + (this_state = next_state);
  964.                   next_state = this_type -> rlx_more;
  965.                 }
  966.             }
  967.             }
  968.           if (growth = this_type -> rlx_length - start_type -> rlx_length)
  969.               fragP -> fr_subtype = this_state;
  970.         }
  971.         break;
  972.  
  973.           default:
  974.         BAD_CASE( fragP -> fr_type );
  975.         break;
  976.           }
  977.         if(growth) {
  978.           stretch += growth;
  979.           stretched++;
  980.         }
  981.       }            /* For each frag in the segment. */
  982.       } while (stretched);    /* Until nothing further to relax. */
  983.   }
  984.  
  985.   /*
  986.    * We now have valid fr_address'es for each frag.
  987.    */
  988.  
  989.   /*
  990.    * All fr_address's are correct, relative to their own segment.
  991.    * We have made all the fixS we will ever make.
  992.    */
  993. }                /* relax_segment() */
  994.  
  995. /*
  996.  * Relax_align. Advance location counter to next address that has 'alignment'
  997.  * lowest order bits all 0s.
  998.  */
  999.  
  1000. static relax_addressT        /* How many addresses does the .align take? */
  1001. relax_align (address, alignment)
  1002.      register relax_addressT    address; /* Address now. */
  1003.      register long int        alignment; /* Alignment (binary). */
  1004. {
  1005.   relax_addressT    mask;
  1006.   relax_addressT    new_address;
  1007.  
  1008.   mask = ~ ( (~0) << alignment );
  1009.   new_address = (address + mask) & (~ mask);
  1010.   return (new_address - address);
  1011. }
  1012.  
  1013. /*
  1014.  *            fixup_segment()
  1015.  */
  1016. static long int
  1017. fixup_segment (fixP, this_segment_type)
  1018.      register fixS *    fixP;
  1019.      int        this_segment_type; /* N_TYPE bits for segment. */
  1020. {
  1021.   register long int        seg_reloc_count;
  1022.         /* JF these all used to be local to the for loop, but GDB doesn't seem to be able to deal with them there, so I moved them here for a bit. */
  1023.       register symbolS *    add_symbolP;
  1024.       register symbolS *    sub_symbolP;
  1025.       register long int        add_number;
  1026.       register int        size;
  1027.       register char *        place;
  1028.       register long int        where;
  1029.       register char        pcrel;
  1030.       register char        baserel;
  1031.       register fragS *        fragP;
  1032.       register int        add_symbol_N_TYPE;
  1033.  
  1034.  
  1035.   seg_reloc_count = 0;
  1036.   for ( ;  fixP;  fixP = fixP -> fx_next)
  1037.     {
  1038.       fragP       = fixP  -> fx_frag;
  1039.       know( fragP );
  1040.       where      = fixP  -> fx_where;
  1041.       place       = fragP -> fr_literal + where;
  1042.       size      = fixP  -> fx_size;
  1043.       add_symbolP = fixP  -> fx_addsy;
  1044.       sub_symbolP = fixP  -> fx_subsy;
  1045.       add_number  = fixP  -> fx_offset;
  1046.       pcrel      = fixP  -> fx_pcrel;
  1047.       baserel      = fixP  -> fx_bsr;
  1048.       if(add_symbolP)
  1049.     add_symbol_N_TYPE = add_symbolP -> sy_type & N_TYPE;
  1050.       if (sub_symbolP)
  1051.     {
  1052.       if(!add_symbolP)    /* Its just -sym */
  1053.         {
  1054.           if(sub_symbolP->sy_type!=N_ABS)
  1055.             as_warn("Negative of non-absolute symbol %s", sub_symbolP->sy_name);
  1056.           add_number-=sub_symbolP->sy_value;
  1057.         }
  1058.       else if (   ((sub_symbolP -> sy_type ^ add_symbol_N_TYPE) & N_TYPE) == 0
  1059.           && (   add_symbol_N_TYPE == N_DATA
  1060.           || add_symbol_N_TYPE == N_TEXT
  1061.           || add_symbol_N_TYPE == N_BSS
  1062.           || add_symbol_N_TYPE == N_ABS))
  1063.         {
  1064.           /* Difference of 2 symbols from same segment. */
  1065.           /* Can't make difference of 2 undefineds: 'value' means */
  1066.           /* something different for N_UNDF. */
  1067.           add_number += add_symbolP -> sy_value - sub_symbolP -> sy_value;
  1068.           add_symbolP = NULL;
  1069.           fixP -> fx_addsy = NULL;
  1070.         }
  1071.       else
  1072.         {
  1073.           /* Different segments in subtraction. */
  1074.           know( sub_symbolP -> sy_type != (N_ABS | N_EXT))
  1075.         if (sub_symbolP -> sy_type == N_ABS)
  1076.             add_number -= sub_symbolP -> sy_value;
  1077.         else
  1078.           {
  1079.                as_warn("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
  1080.                     seg_name[(int)N_TYPE_seg[sub_symbolP->sy_type&N_TYPE]],
  1081.                     sub_symbolP -> sy_name, fragP -> fr_address + where);
  1082.           }
  1083.         }
  1084.     }
  1085.       if (add_symbolP)
  1086.     {
  1087.       if (add_symbol_N_TYPE == this_segment_type && pcrel)
  1088.         {
  1089.           /*
  1090.            * This fixup was made when the symbol's segment was
  1091.            * SEG_UNKNOWN, but it is now in the local segment.
  1092.            * So we know how to do the address without relocation.
  1093.            */
  1094.           add_number += add_symbolP -> sy_value;
  1095.           add_number -=
  1096. #ifndef NS32K
  1097.               size +
  1098. #endif
  1099.               where + fragP -> fr_address;
  1100. #if defined(NS32K) && defined(SEQUENT_COMPATABILITY)
  1101.           if (fragP->fr_baserel)
  1102.             add_number -= 0x12;    /* FOO Kludge alert! */
  1103. #endif
  1104.         /* Kenny thinks this needs *
  1105.         /* add_number +=size-2; */
  1106.           pcrel = 0;    /* Lie. Don't want further pcrel processing. */
  1107.           fixP -> fx_addsy = NULL; /* No relocations please. */
  1108.           /*
  1109.            * It would be nice to check that the address does not overflow.
  1110.            * I didn't do this check because:
  1111.            * +  It is machine dependent in the general case (eg 32032)
  1112.            * +  Compiler output will never need this checking, so why
  1113.            *    slow down the usual case?
  1114.            */
  1115.         }
  1116.       else
  1117.         {
  1118.           switch (add_symbol_N_TYPE)
  1119.         {
  1120.         case N_ABS:
  1121.           add_number += add_symbolP -> sy_value;
  1122.           fixP -> fx_addsy = NULL;
  1123.           add_symbolP = NULL;
  1124.           break;
  1125.           
  1126.         case N_BSS:
  1127.         case N_DATA:
  1128.         case N_TEXT:
  1129.           seg_reloc_count ++;
  1130.           add_number += add_symbolP -> sy_value;
  1131.           break;
  1132.           
  1133.         case N_UNDF:
  1134.           seg_reloc_count ++;
  1135.           break;
  1136.           
  1137.         default:
  1138.           BAD_CASE( add_symbol_N_TYPE );
  1139.           break;
  1140.         }        /* switch on symbol seg */
  1141.         }            /* if not in local seg */
  1142.     }            /* if there was a + symbol */
  1143.       if (pcrel)
  1144.     {
  1145.       add_number -=
  1146. #ifndef NS32K
  1147.           size + 
  1148. #endif
  1149.           where + fragP -> fr_address;
  1150.       if (add_symbolP == 0)
  1151.         {
  1152.           fixP -> fx_addsy = & abs_symbol;
  1153.           seg_reloc_count ++;
  1154.         }
  1155.     }
  1156.  
  1157.       if (baserel && add_number)    /* ###mw is that it....??? */
  1158.         add_number -= text_last_frag->fr_address;
  1159.  
  1160.       /* OVE added fx_im_disp for ns32k and others */
  1161.       if (!fixP->fx_bit_fixP) {
  1162.     /* JF I hope this works . . . */
  1163.     if((size==1 && (add_number& ~0xFF)   && (add_number&~0xFF!=(-1&~0xFF))) ||
  1164.        (size==2 && (add_number& ~0xFFFF) && (add_number&~0xFFFF!=(-1&~0xFFFF))))
  1165.       as_warn("Fixup of %d too large for field width of %d",add_number, size);
  1166.  
  1167.     switch (fixP->fx_im_disp) {
  1168.     case 0:
  1169. #if defined(SPARC) || defined(I860)
  1170.       fixP->fx_addnumber = add_number;
  1171.       md_number_to_imm(place, add_number, size, fixP, this_segment_type);
  1172. #else
  1173.       md_number_to_imm (place, add_number, size);
  1174.       /* OVE: the immediates, like disps, have lsb at lowest address */
  1175. #endif
  1176.       break;
  1177.     case 1:
  1178.       md_number_to_disp (place,
  1179.                  fixP->fx_pcrel ? add_number+fixP->fx_pcrel_adjust:add_number,
  1180.                  size);
  1181.       break;
  1182.     case 2: /* fix requested for .long .word etc */
  1183.       md_number_to_chars (place, add_number, size);
  1184.       break;
  1185.     default:
  1186.       as_fatal("Internal error in write.c in fixup_segment");
  1187.     } /* OVE: maybe one ought to put _imm _disp _chars in one md-func */
  1188.       } else {
  1189.     md_number_to_field (place, add_number, fixP->fx_bit_fixP);
  1190.       }
  1191.     }                /* For each fixS in this segment. */
  1192.   return (seg_reloc_count);
  1193. }                /* fixup_segment() */
  1194.  
  1195.  
  1196. /* The sparc needs its own emit_relocations() */
  1197. #if !defined(SPARC) && !defined(I860)
  1198. /*
  1199.  *        emit_relocations()
  1200.  *
  1201.  * Crawl along a fixS chain. Emit the segment's relocations.
  1202.  */
  1203. static void
  1204. emit_relocations (fixP, segment_address_in_file)
  1205.      register fixS *    fixP;    /* Fixup chain for this segment. */
  1206.      relax_addressT    segment_address_in_file;
  1207. {
  1208.   struct relocation_info    ri;
  1209.   register symbolS *        symbolP;
  1210.  
  1211.     /* JF this is for paranoia */
  1212.   bzero((char *)&ri,sizeof(ri));
  1213.   for ( ;  fixP;  fixP = fixP -> fx_next)
  1214.     {
  1215.       if (symbolP = fixP -> fx_addsy)
  1216.     {
  1217.  
  1218.         /* These two 'cuz of NS32K */
  1219.       ri . r_baserel        = fixP -> fx_bsr;
  1220. #ifdef NS32K
  1221.       ri . r_disp        = fixP -> fx_im_disp;
  1222. #endif
  1223.  
  1224.       ri . r_length        = nbytes_r_length [fixP -> fx_size];
  1225.       ri . r_pcrel        = fixP -> fx_pcrel;
  1226.       ri . r_address    = fixP -> fx_frag -> fr_address
  1227.         +   fixP -> fx_where
  1228.           - segment_address_in_file;
  1229.       if ((symbolP -> sy_type & N_TYPE) == N_UNDF)
  1230.         {
  1231.           ri . r_extern    = 1;
  1232.           ri . r_symbolnum    = symbolP -> sy_number;
  1233.         }
  1234.       else
  1235.         {
  1236.           ri . r_extern    = 0;
  1237.           ri . r_symbolnum    = symbolP -> sy_type & N_TYPE;
  1238.         }
  1239.  
  1240.       /* 
  1241.         The 68k machines assign bit-fields from higher bits to 
  1242.         lower bits ("left-to-right") within the int.  VAXen assign 
  1243.         bit-fields from lower bits to higher bits ("right-to-left").
  1244.         Both handle multi-byte numbers in their usual fashion
  1245.         (Big-endian and little-endian stuff).
  1246.         Thus we need a machine dependent routine to make
  1247.         sure the structure is written out correctly.  FUN!
  1248.        */
  1249.       md_ri_to_chars((char *) &ri, ri); 
  1250.       append (&next_object_file_charP, (char *)& ri, (unsigned long)sizeof(ri));
  1251.     }
  1252.     }
  1253.  
  1254. }
  1255. #endif
  1256.  
  1257. int
  1258. is_dnrange(f1,f2)
  1259. struct frag *f1,*f2;
  1260. {
  1261.     while(f1) {
  1262.         if(f1->fr_next==f2)
  1263.             return 1;
  1264.         f1=f1->fr_next;
  1265.     }
  1266.     return 0;
  1267. }
  1268. /* End: as-write.c */
  1269.